package cn.swust.service.serviceimpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.swust.model.MessageBean;
import cn.swust.service.MessageService;
import cn.swust.util.DBUtil;

public class MessageServiceImpl implements MessageService {

	public int addMes(MessageBean mes) {
		int ret = 0;
		try{
		String insertSql = "insert into message(mess_title,mess_quetion,mess_author,mess_answer" + 
		",mess_questime,mess_anstime,mess_ansauther) values ('" + mes.getTitle() + "','" + 
		mes.getQuestion() + "','" + mes.getAuthor() + "','" + mes.getAnswer() +
		"','" + mes.getQuesTime() + "','" + mes.getAnsTime() + "','" + mes.getAnsAuthor() + "')";
		Connection conn = DBUtil.getConnection();
		Statement state = conn.createStatement();
		state.execute(insertSql, Statement.RETURN_GENERATED_KEYS);
		ResultSet rs = state.getGeneratedKeys();
		while(rs.next())
			ret = rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return ret;
	}

	public int delMesById(int id) {
		String delSql = "delete from message where mess_id = " + id;
		return DBUtil.updateSql(delSql);
	}

	public int modMes(int id, MessageBean mes) {
		String modSql = "update message set mess_title = '" + mes.getTitle() + "',mess_quetion = '" +
		mes.getQuestion() + "',mess_author = '" + mes.getAuthor() + "',mess_answer = '" + mes.getAnswer() +
		"',mess_questime = '" + mes.getQuesTime() + "',mess_anstime = '" + mes.getAnsTime() +
		"',mess_ansauther = '" + mes.getAnsAuthor() + "' where mess_id = " + id;
		return DBUtil.updateSql(modSql);
	}

	public MessageBean queryById(int id) {
		String querySql = "select * from message where mess_id = " + 3;
		ResultSet rs = DBUtil.querySql(querySql, DBUtil.creatStatement(DBUtil.getConnection()));
		MessageBean mes = new MessageBean();
		try {
			while (rs.next()) {
				mes.setId(rs.getInt("mess_id"));
				mes.setTitle(rs.getString("mess_title"));
				mes.setQuestion(rs.getString("mess_quetion"));
				mes.setAuthor(rs.getString("mess_author"));
				mes.setAnswer(rs.getString("mess_answer"));
				mes.setQuesTime(rs.getString("mess_questime"));
				mes.setAnsTime(rs.getString("mess_anstime"));
				mes.setAnsAuthor(rs.getString("mess_ansauther"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mes;
	}

	public MessageBean queryByTitle(String title) {
		String querySql = "select * from message where mess_title = '" + title + "'";
		ResultSet rs = DBUtil.querySql(querySql, DBUtil.creatStatement(DBUtil.getConnection()));
		MessageBean mes = new MessageBean();
		try {
			while (rs.next()) {
				mes.setId(rs.getInt("mess_id"));
				mes.setTitle(rs.getString("mess_title"));
				mes.setQuestion(rs.getString("mess_quetion"));
				mes.setAuthor(rs.getString("mess_author"));
				mes.setAnswer(rs.getString("mess_answer"));
				mes.setQuesTime(rs.getString("mess_questime"));
				mes.setAnsTime(rs.getString("mess_anstime"));
				mes.setAnsAuthor(rs.getString("mess_ansauther"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return mes;
	}

}
